package com.xcc.shopanim;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * Created by Administrator on 2017/12/19.
 */
public class BaseFragment<T extends BaseContract.Presenter> extends Fragment {
    private static final String TAG = "BaseFragment";
    protected Context mContext;
    protected T presenter;
    public boolean isPause, hidden, isDestroyView, isResume;
    protected OnFragmentCallBack onFragmentCallBack, onFragmentCallBack2;

    public BaseFragment<T> getFragment() {
        return this;
    }

    public BaseFragment() {
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isDestroyView = false;
    }

    public void sendMessage(int type, Object object) {
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        if (context instanceof OnFragmentCallBack) {
            onFragmentCallBack = (OnFragmentCallBack) context;
        }
    }

    /**
     * 这是设置 onFragmentCallBack2
     */
    public BaseFragment<T> setOnFragmentCallBack(OnFragmentCallBack onFragmentCallBack) {
        this.onFragmentCallBack2 = onFragmentCallBack;
        return this;
    }

    public void onResume() {
        isResume = true;
        isPause = false;
        super.onResume();
    }

    public void onPause() {
        isResume = false;
        isPause = true;
        super.onPause();
    }

    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) presenter.onDestroy();
        presenter = null;
        onFragmentCallBack = null;
    }

    public void onDestroyView() {
        isDestroyView = true;
        super.onDestroyView();
    }

    public boolean onBackPressed() {
        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    public void onHiddenChanged(boolean hidden) {
        this.hidden = hidden;
    }

    public interface OnFragmentCallBack {
        Object onFragmentCallBack(BaseFragment fragment, int type, Object object);
    }
}
